<?php

namespace catchAdmin\hunqing\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchAuth;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\hunqing\model\Userinfos as UserinfosModel;
use catcher\exceptions\FailedException;
use think\facade\Db;
use think\Response;

class Userinfos extends CatchController
{

    protected $userinfosModel;

    /**
     *
     * @time 2025/04/28 20:52
     * @param UserinfosModel $userinfosModel
     * @return mixed
     */
    public function __construct(UserinfosModel $userinfosModel)
    {
        $this->userinfosModel = $userinfosModel;
    }

    /**
     *
     * @time 2025/04/28 20:52
     * @return Response
     */
    public function index(CatchAuth $auth): Response
    {

        return CatchResponse::paginate($this->userinfosModel->getList($auth->user()->id));
    }

    /**
     *
     * @time 2025/04/28 20:52
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        return CatchResponse::success($this->userinfosModel->storeBy($request->post()));
    }

    /**
     *
     * @time 2025/04/28 20:52
     * @param $id
     * @return Response
     */
    public function read($id): Response
    {
        return CatchResponse::success($this->userinfosModel->findBy($id));
    }

    /**
     *
     * @time 2025/04/28 20:52
     * @param $id
     * @param Request $request
     * @return Response|\think\response\Json
     */
    public function update($id, Request $request,CatchAuth $auth)
    {
        if ($auth->user()->id == 1){
            return CatchResponse::fail('管理员不要修改会员资料，以免引起错误');
        }


        $cid = $this->userinfosModel->where('id',$id)->value('creator_id');
        if ($cid != $auth->user()->id && $auth->user()->id != 1){
            return CatchResponse::fail('权限不够');
        }
        $data = $request->post();
        if (!empty($data['sex'])){
            if ($data['sex'] == '男') {
                $data['sex'] = 1;
            }

            if ($data['sex'] == '女') {
                $data['sex'] = 0;
            }
        }
        return CatchResponse::success($this->userinfosModel->updateBy($id, $data));
    }

    /**
     *
     * @time 2025/04/28 20:52
     * @param $id
     * @return Response
     */
    public function delete($id): Response
    {
        return CatchResponse::success($this->userinfosModel->deleteBy($id));
    }


    public function addinfo(CatchAuth $auth)
    {
        $data['info'] = request()->post('field101');
        $data['uid'] = request()->post('uid');
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['user'] =$auth->user()->username;

        Db::name('userinfos')->where('id', $data['uid'])->update(['updated_at' => time()]);

        Db::name('info')->insert($data);
        return CatchResponse::success();
    }

    public function info()
    {
        $data = Db::name('info')->where('uid', request()->param('id'))->order('created_at', 'desc')->select()->toArray();
        return CatchResponse::success($data);
    }


    public function addinfos(CatchAuth $auth)
    {


        $phone = Db::name('userinfos ')->where('lianxi', request()->param('lianxi'))->value('id');

        if ($phone) {
            throw new FailedException("手机号已存在");
        }


        $data = \think\facade\Request::post();
        if (!empty($data['fileLists1'][0]['url'])){
            $data['image'] = $data['fileLists1'][0]['url'];

        }
        if (!empty($data['fileLists2'][0]['url'])){
            $data['image1'] = $data['fileLists2'][0]['url'];

        }
        if (!empty($data['fileLists3'][0]['url'])){
            $data['image2'] = $data['fileLists3'][0]['url'];

        }
        if (!empty($data['fileLists4'][0]['url'])){
            $data['image3'] = $data['fileLists4'][0]['url'];

        }
        if (!empty($data['fileLists5'][0]['url'])){
            $data['image4'] = $data['fileLists5'][0]['url'];

        }

        $data['che'] = $data['che'] == 'false'?1:0;
        $data['chedai'] = $data['chedai'] == 'false'?1:0;
        $data['fang'] = $data['fang'] == 'false'?1:0;
        $data['fangdai'] = $data['fangdai'] == 'false'?1:0;
        $data['isin'] = $data['isin'] == 'false'?1:0;
        $data['vip'] = $data['vip'] == 'false'?0:1;


        unset($data['fileLists1']);
        unset($data['fileLists2']);
        unset($data['fileLists3']);
        unset($data['fileLists4']);
        unset($data['fileLists5']);

        $data['created_at'] = time();
        $data['creator_id'] = $auth->user()->id;




        Db::name('userinfos')->insert($data);
        return CatchResponse::success();

    }

    public function addinfos1($id,CatchAuth $auth)
    {
        $data = \think\facade\Request::post();
        if (!empty($data['fileLists1'][0]['url'])){
            $data['image'] = $data['fileLists1'][0]['url'];

        }
        if (!empty($data['fileLists2'][0]['url'])){
            $data['image1'] = $data['fileLists2'][0]['url'];

        }
        if (!empty($data['fileLists3'][0]['url'])){
            $data['image2'] = $data['fileLists3'][0]['url'];

        }
        if (!empty($data['fileLists4'][0]['url'])){
            $data['image3'] = $data['fileLists4'][0]['url'];

        }
        if (!empty($data['fileLists5'][0]['url'])){
            $data['image4'] = $data['fileLists5'][0]['url'];

        }

        $data['che'] = $data['che'] == 'false'?1:0;
        $data['chedai'] = $data['chedai'] == 'false'?1:0;
        $data['fang'] = $data['fang'] == 'false'?1:0;
        $data['fangdai'] = $data['fangdai'] == 'false'?1:0;
        $data['isin'] = $data['isin'] == 'false'?1:0;
        $data['vip'] = $data['vip'] == 'false'?0:1;


        unset($data['fileLists1']);
        unset($data['fileLists2']);
        unset($data['fileLists3']);
        unset($data['fileLists4']);
        unset($data['fileLists5']);

        $data['created_at'] = time();
        $data['creator_id'] = $auth->user()->id;




        Db::name('userinfos')->where('id', $id)->update($data);
        return CatchResponse::success();

    }
}